<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <style>
        *{ box-sizing: border-box; }
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .login-box {
            width: 350px;
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            display: flex;
            flex-direction: column;
            align-items: flex-start; 
        }
        .login-box .form-row {
            display: flex;
            margin-bottom: 15px;
        }
        .login-box .form-row label {
            flex: 0 0 100px; 
            margin-right:10px;
        }
        .login-box .form-row input[type="text"],
        .login-box .form-row input[type="password"] {
            flex: 1;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }
        .login-box .form-actions {
            display: flex;
            justify-content: space-between;
            margin-top: 20px;
        }
        .login-box .form-actions input[type="submit"] {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        .login-box .form-actions input[type="submit"]:hover {
            background-color: #45a049;
        }
        .login-box .form-actions a {
            color: #4CAF50;
            text-decoration: none;
            font-weight: bold;
        }
        .login-box .form-actions a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="login-box">
        <form id="loginForm">
            <div class="form-row">
                <label for="username">用户名：</label>
                <input type="text" id="username" name="username" required>
            </div>
            <div class="form-row">
                <label for="password">密码：</label>
                <input type="password" id="password" name="password" required>
            </div>
            <div class="form-actions">
                <input type="submit" value="登录">
                <a href="register.html">注册</a>
            </div>
        </form>
    </div>

    <script>
        document.getElementById('loginForm').addEventListener('submit', function(event) {
            event.preventDefault(); // 阻止表单的默认提交行为
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
        
            fetch('http://localhost:3000/user/login', { // 确保URL与后端路由匹配
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ username, password })
            })
            .then(response => response.json())
            .then(data => {
                if (data.status === 0) {
                    alert('登录成功！');
                    // 重定向到待办事项管理页面
                    window.location.href = 'task-manager.html';
                } else {
                    alert('登录失败：' + data.msg);
                }
            })
            .catch(error => {
                console.error('登录请求失败:', error);
                alert('登录请求失败，请稍后重试。');
            });
        });
        document.getElementById('loginForm').addEventListener('submit', function(event) {
            event.preventDefault(); // 阻止表单的默认提交行为
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;

            // 假设默认登录账号为admin，密码为ddd
            if (username === 'admin' && password === 'ddd') {
                alert('登录成功！');
                // 重定向到待办事项管理页面
                window.location.href = 'task-manager.html'; // 请确保文件名正确
            } else {
                alert('登录失败：用户名或密码错误');
            }
        });
    </script>
</body>
</html>